15. Demo 1: Cross-Validation and the Train-Test Split

Cd13652 C6 L2 Demo 1 V3

Cross-Validation and Data Preparation

Data Preparation Steps

  • Utilize the California Housing dataset.
  • Clean data: Handle one categorical feature and 207 missing values.
  • Encode: Use get_dummies in Pandas for the ocean_proximity feature.
  • Remove Outliers: Drop rows with a median house value over $500,000.
  • Missing Values: Impute missing total bedroom values post-train/test split to avoid data leakage.

Train/Test split

  • Define feature matrix X and target y.
  • Split data into train (80%) and test (20%) sets using scikit-learn.
  • Shuffle data since it lacks temporal dimension.
  • Impute missing values with the mean or median using scikit-learn's SimpleImputer.
  • Apply feature scaling with StandardScaler post split.

Cross-Validation

  • Use train data to carve out a validation set using k-fold cross-validation.
  • Train a linear regression on the training set, measure using the R^2 score.
  • Compare results using five-fold cross-validation for a better accuracy estimate.
  • Achieve a more reliable output due to training and testing on multiple subsets.